package com.ysyc.driver_ee.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.ysyc.driver_ee.R;
import com.ysyc.driver_ee.activity.MainActivity;
import com.ysyc.driver_ee.base.BaseFragment;
import com.ysyc.driver_ee.widget.TitleView;

import butterknife.BindView;

import static com.ysyc.driver_ee.fragment.CapacityFragment.TYPE_KEY;

/**
 * <p>描述：(这里用一句话描述这个类的作用)</p>
 * 作者： Chan<br>
 * 日期： 2018/10/29 17:57<br>
 * 版本： v1.0<br>
 */
public class OrderFormFragment extends BaseFragment {
    @BindView(R.id.title_order_form_view)
    TitleView titleOrderFormView;
    @BindView(R.id.tl_order_form_view)
    TabLayout tlOrderFormView;
    @BindView(R.id.vp_order_form_view)
    ViewPager vpOrderFormView;
    private String[] TAB_TITLES = new String[]{"未处理", "已接单", "已派单"};
    //Tab 数目
    private int COUNT = TAB_TITLES.length;
    //Fragment 数组
    private BaseFragment[] TAB_FRAGMENTS = new BaseFragment[COUNT];
    public static final String IDENTITY = "identity";

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_order_form;
    }

    @Override
    protected void onFragmentCreate(View view) {
        super.onFragmentCreate(view);
        initView();

    }

    private void initView() {
        titleOrderFormView.setTitle("订单",getResources().getColor(R.color.white_ff));
        Integer identity = ((MainActivity) getActivity()).driverInfo.identity;
//        司机身份 0为司机 1为负责人
        if (0 == identity) {
            TAB_TITLES = new String[]{"未处理", "已接单"};
            COUNT = TAB_TITLES.length;
            TAB_FRAGMENTS = new BaseFragment[COUNT];
        } else if (1 == identity) {
            TAB_TITLES = new String[]{"未处理", "已接单", "已派单"};
            COUNT = TAB_TITLES.length;
            TAB_FRAGMENTS = new BaseFragment[COUNT];
        }
        initTabLayout();
    }


    private void initTabLayout() {
        for (int i = 0; i < COUNT; i++) {
            Bundle bundle = new Bundle();
            bundle.putInt(TYPE_KEY, i );
            bundle.putInt(IDENTITY,((MainActivity)getActivity()).driverInfo.identity);
            if (i < 2) {
                IncompleteOrdersFragment fragment = IncompleteOrdersFragment.newInstance(IncompleteOrdersFragment.class, bundle);
                TAB_FRAGMENTS[i] = fragment;
            } else {
                OrderCompletedFragment fragment = OrderCompletedFragment.newInstance(OrderCompletedFragment.class, bundle);
                TAB_FRAGMENTS[i] = fragment;
            }
            tlOrderFormView.addTab(tlOrderFormView.newTab().setText(TAB_TITLES[i]));
        }

        OrderFormFragment.MyViewPagerAdapter mAdapter = new OrderFormFragment.MyViewPagerAdapter(getChildFragmentManager());
        vpOrderFormView.setAdapter(mAdapter);
        vpOrderFormView.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tlOrderFormView));
        tlOrderFormView.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(vpOrderFormView));
        vpOrderFormView.setOffscreenPageLimit(2);
    }


    /**
     * @description: ViewPager 适配器
     */
    private class MyViewPagerAdapter extends FragmentPagerAdapter {

        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return TAB_FRAGMENTS[position];
        }

        @Override
        public int getCount() {
            return TAB_TITLES.length;
        }

    }

}
